<template>
	<view class="login">
		<image class="bg" src="/static/bg.png"></image>
		<view class="loginBtn">
			<image src="/static/wechat.png" mode="" class="wechat" @click="wechatLogin"></image>
			微信登录
			<view class="agreement">
				<u-checkbox-group
					v-model="agreement"
				>
					<u-checkbox
						name="item.name"
					>
					</u-checkbox>
				</u-checkbox-group> 
				阅读并同意 <navigator class="navigator" url="/pages/webview/webview?type=3">《用户协议》</navigator>
			</view>
		</view>
	</view>
	<updateModal></updateModal>
	
</template>

<script>
	export default {
		data() {
			return {
				agreement: []
			};
		},
		methods: {
			wechatLogin() {
				if(!this.agreement || !this.agreement.length) {
					uni.showModal({
						title:'提醒',
						content:"请同意用户协议！",
						success: res => {
							if(res.confirm) {
								this.agreement = ['item.name']
								this.toWechat()
							}
						}
					})
				}else {
					this.toWechat()
				}
			},
			toWechat() {
				uni.login({
					provider: 'weixin',
					success: res => {
						// console.log(res,'weichat login')
						uni.showLoading({
							title: '登录中...'
						})
						const { access_token, openid, unionid } = res.authResult;
						if(!unionid || !access_token || !openid) {
							uni.hideLoading()
							// 有时候会失败，尤其是打开app第一次授权的时候，再来一次就好
							this.toWechat()
							return
						}
						uni.request({
							url: `https://api.weixin.qq.com/sns/userinfo?access_token=${access_token}&openid=${openid}`,
							method:'get',
						}).then(res => {
							// console.log(res,'userinfo')
							const { nickname, sex, headimgurl } = res.data;
							this.$request({
								url:'/api/v1/miletus/user/func/logOrReg',
								method:'post',
								needLogin: false,
								data: {
									openid,unionid,nickname,sex,headimgurl,chanType: 1
								}
							}).then(res => {
								console.log(res,'login success')
								this.$store.commit('setToken',res.token)
								getApp().globalData.hasLogin = true
								this.$nextTick(() => {
									this.$store.dispatch('getUserInfo').then(res => {
										uni.hideLoading()
										uni.switchTab({
											url: '/pages/index/index'
										})
									})
								})
							}).catch(() => {
								uni.showToast({
									title: '异常错误，请重试！',
									icon: 'none'
								})
								uni.hideLoading()
							})
						}).catch(err => {
							uni.showToast({
								title: '异常错误，请重试！',
								icon: 'none'
							})
							uni.hideLoading()
						})
					},
					fail: err => {
						wx.showModal({
							title:'错误',
							content: JSON.stringify(err)
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
.login {
	height: 100vh;
	position: relative;
	display: flex;
	justify-content: center;
	align-items: center;
	.bg {
		position: absolute;
		width: 100%;
		height: 100%;
		left: 0%;
		top: 0%;
		z-index: 1;
	}
	.loginBtn {
		position: relative;
		z-index: 2;
		text-align: center;
		font-size: 26rpx;
		.wechat {
			$s: 100rpx;
			width: $s;
			height: $s;
			margin: 0 auto 20rpx;
			display: block;
		}
		.agreement {
			margin-top: 20rpx;
			text-align: center;
			display: flex;
			justify-content: center;
			align-items: center;
			font-size: 24rpx;
			.navigator {
				color: #ffaa00;
			}
		}
	}
}
</style>
